//
//  TextFieldViewController.m
//  MClient
//
//  Created by  史正烨 on 09-11-8.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TextFieldViewController.h"
#import "RegexKitLite.h"
@implementation TextFieldViewController
@synthesize valueField;
@synthesize fieldData;
@synthesize field;
@synthesize isReadOnly;
@synthesize webView;

- (int)identiferId {
	return 0;
}
- (id)init {
	if (self = [super init]) {
		self.hidesBottomBarWhenPushed = YES;
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	
	self.title = [self.field objectForKey:@"lbl"];
	
	[self rightBarButton:@"确定" withStyle:UIBarButtonItemStyleDone selector:@selector(selectAction:)];
	[self leftBarButton:@"取消" withStyle:UIBarButtonItemStylePlain selector:@selector(selectCancelAction:)];
	
	if(self.width > self.height) {
		textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 140)]; //键盘高度
	} else {
		textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 216)]; //键盘高度
	}
	textView.font = [UIFont systemFontOfSize:16];
	if ([@"0" isEqualToString:[field objectForKey:@"type"]]) {
		NSString *value = [fieldData objectForKey:@"value"];
		NSString *textValue = [value stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
		textValue = [textValue stringByReplacingOccurrencesOfString:@"<BR>" withString:@"\n"];
		textValue = [textValue stringByReplacingOccurrencesOfRegex:@"<[\\s]*?(?i)script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(?i)script[\\s]*?>" withString:@""];
		textValue = [textValue stringByReplacingOccurrencesOfRegex:@"<[\\s]*?(?i)style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(?i)style[\\s]*?>" withString:@""];
		textValue = [textValue stringByReplacingOccurrencesOfRegex:@"<[^>]+>" withString:@""];
		textValue = [textValue stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
		textValue = [textValue stringByReplacingOccurrencesOfString:@"&gt;" withString:@""];
		textValue = [textValue stringByReplacingOccurrencesOfString:@"&lt;" withString:@""];
		[textValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
		NSArray *valueArray = [textValue componentsSeparatedByString:@"\n"];
		NSMutableString *valueBuffer = [[NSMutableString alloc] init];
		for (int i = 0; i < [valueArray count]; i++) {
			if ([@"" isEqualToString:[[valueArray objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]) {
				continue;
			}
			[valueBuffer appendString:[valueArray objectAtIndex:i]];
			[valueBuffer appendString:@"\n\n"];
		}
		textView.text = valueBuffer;		
	}else {
		textView.text = valueField.text;
	}
	textView.autoresizingMask = nMCViewAutoresizingMask | UIViewAutoresizingFlexibleHeight;
	textView.scrollEnabled = YES;
	textView.editable = !isReadOnly;
	textView.contentInset = UIEdgeInsetsMake(0, 0, 3, 0);

	[self.clientView addSubview:textView];
	
	if (!isReadOnly) {
		[textView becomeFirstResponder];
	}
}


- (void)dealloc {
	[valueField release];
	[fieldData release];
	[field release];
	[textView release];
	[webView release];
    [super dealloc];
}

#pragma mark ---- 按钮事件处理方法 ----
- (void) selectAction: (id) sender {
	if ([@"0" isEqualToString:[field objectForKey:@"type"]]) {
		NSString *textValue = [textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
		if (textValue == nil) {
			textValue = @"";
		}
		[webView loadHTMLString:textValue baseURL:nil];
		[fieldData setObject:textValue forKey:@"value"];
	}else {
		self.valueField.text = textView.text;
		[fieldData setObject:textView.text==nil?@"":textView.text forKey:@"value"];
	}
	[self.navigationController popViewControllerAnimated:YES];
}

- (void) selectCancelAction: (id) sender {
	[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark keyboard notifications

// Called when the UIKeyboardWillShowNotification is sent.
- (void)keyboardDidShown:(NSNotification*)aNotification
{
	NSDictionary* info = [aNotification userInfo];
	
    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];
    CGSize keyboardSize = [aValue CGRectValue].size;
	
	textView.frame = CGRectMake(0, 0, self.width, self.height-keyboardSize.height);
}


// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardDidHidden:(NSNotification*)aNotification
{
	textView.frame = CGRectMake(0, 0, self.width, self.height);
}
@end
